package com.acce.gxsc.page.registerCar.selectGoods;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.acce.gxsc.R;
import com.acce.gxsc.model.netBean.GoodsBean;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.widget.adapterrv.CommonAdapter;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huxley on 2017/5/26.
 */

public class SelectGoodsActivity extends BaseMvpActivity<SelectGoodsView, SelectGoodsPresenter> implements SelectGoodsView {

    @Bind(R.id.rv_father)
    RecyclerView mRvFather;
    @Bind(R.id.rv_son)
    RecyclerView mRvSon;

    ArrayList<GoodsBean> mGoodsTypeList = new ArrayList<>();

    CommonAdapter<GoodsBean> mCompanyAdapter1;
    CommonAdapter<GoodsBean> mCompanyAdapter2;

    int position = 0;

    @Override
    protected SelectGoodsPresenter createPresenter() {
        return new SelectGoodsPresenter();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_goods);
        ButterKnife.bind(this);
        init();
        mPresenter.loadGoodsList();
    }

    private void init() {
        mRvFather.setLayoutManager(new LinearLayoutManager(this));
        mRvFather.setAdapter(mCompanyAdapter1 = new CommonAdapter<GoodsBean>(this, R.layout.item_company, mGoodsTypeList) {
            @Override
            protected void convert(ViewHolder holder, final GoodsBean saleSheet, final int position) {
                holder.setText(R.id.tv, saleSheet.sonName);
                holder.setBackgroundColor(R.id.tv, SelectGoodsActivity.this.getResources().getColor(SelectGoodsActivity.this.position == position ? R.color.divider_dark : R.color.white));
                holder.setOnClickListener(R.id.ll_content, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        SelectGoodsActivity.this.position = position;
                        mCompanyAdapter2.getDatas().clear();
                        mCompanyAdapter2.getDatas().addAll(getDatas().get(position).child);
                        mCompanyAdapter2.notifyDataSetChanged();
                        mCompanyAdapter1.notifyDataSetChanged();
                    }
                });
            }
        });
        mRvSon.setLayoutManager(new GridLayoutManager(this, 2));
        mRvSon.setAdapter(mCompanyAdapter2 = new CommonAdapter<GoodsBean>(this, R.layout.item_company, new ArrayList<GoodsBean>()) {
            @Override
            protected void convert(ViewHolder holder, final GoodsBean saleSheet, final int position) {
                holder.setText(R.id.tv, saleSheet.sonName);
                holder.setOnClickListener(R.id.ll_content, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        intent.putExtra("father", saleSheet.fatherName);
                        intent.putExtra("son", saleSheet.sonName);
                        intent.putExtra("goodsId", saleSheet.goodsId);
                        SelectGoodsActivity.this.setResult(Activity.RESULT_OK, intent);
                        SelectGoodsActivity.this.finish();
                    }
                });
            }
        });
    }

    @Override
    public void loadSuccess(List<GoodsBean> result) {
        mCompanyAdapter1.getDatas().clear();
        mCompanyAdapter1.getDatas().addAll(result);
        mCompanyAdapter2.getDatas().clear();
        mCompanyAdapter2.getDatas().addAll(result.get(0).child);
        mCompanyAdapter1.notifyDataSetChanged();
        mCompanyAdapter2.notifyDataSetChanged();
    }
}
